/*
  ==============================================================================

   This file is part of the JUCE library - "Jules' Utility Class Extensions"
   Copyright 2004-11 by Raw Material Software Ltd.

  ------------------------------------------------------------------------------

   JUCE can be redistributed and/or modified under the terms of the GNU General
   Public License (Version 2), as published by the Free Software Foundation.
   A copy of the license is included in the JUCE distribution, or can be found
   online at www.gnu.org/licenses.

   JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  ------------------------------------------------------------------------------

   To release a closed-source product which uses JUCE, commercial licenses are
   available: visit www.rawmaterialsoftware.com/juce for more information.

  ==============================================================================
*/

extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component*, void* parent);

//==============================================================================
class D3D9Context::NativeContext
{
public:
    NativeContext (Component& component,
                   const D3D9PixelFormat& pixelFormat)
    {
        createNativeWindow (component);

 
        component.getTopLevelComponent()->repaint();
        component.repaint();
    }

    ~NativeContext()
    {
        deleteRenderContext();
        releaseDC();
    }

	HWND GetHandle() { return (HWND) (nativeWindow->getNativeHandle()); }

    void initialiseOnRenderThread() 
	{

	}

    void shutdownOnRenderThread() 
	{

	}

    bool makeActive() const noexcept        { return true; }


    void updateWindowPosition (const Rectangle<int>& bounds)
    {
        if (nativeWindow != nullptr)
            SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
                          bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
                          SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);

    }


    bool createdOk() const noexcept                 { return true; }
    LPDIRECT3DSURFACE9 getFrameBufferSurface() const noexcept  { return NULL; }

    struct Locker { Locker (NativeContext&) {} };

private:
    Component dummyComponent;
    ScopedPointer<ComponentPeer> nativeWindow;

    HDC dc;
	int nativeWindowWidth;
	int nativeWindowHeight;


    void createNativeWindow (Component& component)
    {
        Component* topComp = component.getTopLevelComponent();
        nativeWindow = createNonRepaintingEmbeddedWindowsPeer (&dummyComponent, topComp->getWindowHandle());
        updateWindowPosition (topComp->getLocalArea (&component, component.getLocalBounds()));
        nativeWindow->setVisible (true);
        dc = GetDC ((HWND) nativeWindow->getNativeHandle());
    }

    void deleteRenderContext()
    {

    }

    void releaseDC()
    {
        ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
    }
	
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext);
};



bool D3D9Helpers::isContextActive()
{
    return true;
}
//==============================================================================

